//struct and enum
//常量（constant）可以通过 const 和 static 关键字来创建
//static 关键字定义的静态常量是模块范围的
mod my_module {
    static mut COUNTER: u32 = 0;
}
#[derive(Debug, Default)]
struct Person {
    name: String,
    age: u8,
}
// impl Default for Person {
//     fn default() -> Self {
//         Person {
//             name: String::from("default"),
//             age: 0,
//         }
//     }
// }

struct Unit;
//省略字段的struct,通过序号索引每个字段
#[derive(Debug)]
struct Pair (f32, f32);
#[derive(Debug)]
struct Point {
    x: f32,
    y: f32,
    z: f32,
}
#[derive(Debug)]
struct Retangle {
    left_top: Point,
    right_bottom: Point,
}

enum WebEvent {
    //单元结构体
    PageLoad,
    PageUnload,
    //元组结构体
    KeyPress(char),
    Paste(String),
    //普通结构体
    Click{x:i32, y: i32},
}

fn inspect(event: WebEvent) {
    match event{
        WebEvent::PageLoad => println!("page load"),
        WebEvent::PageUnload => println!("page unload"),
        WebEvent::KeyPress(c) => println!("key press: {}", c),
        WebEvent::Paste(s) => println!("paste: {}", s),
        WebEvent::Click{x, y} => println!("click at ({}, {})", x, y),
        
    }
}

fn test_enum() {
    let pageload = WebEvent::PageLoad;
    let keyPress = WebEvent::KeyPress('x');
    let paste = WebEvent::Paste("hello world".to_owned());
    let click = WebEvent::Click { x: 10, y: 20 };
    inspect(pageload);
    inspect(keyPress);
    inspect(paste);
    inspect(click);
}

fn test_struct() {

    let p1 = Person { name:String::from("yang"), age: 28};
    println!("{:?}", p1);
    // let p2 = Person;
    let point = Point{x: 1.0, y: 2.0, z: 3.0};
    println!("{:?}", point);

    let retangle = Retangle{
        left_top: Point{x: 1.0, y: 2.0, z: 3.0},
        right_bottom: Point{x: 4.0, y: 5.0, z: 6.0},
    };
    println!("{:?}", retangle);

    let Point{x, y, z} = point;
    
    println!("解绑{}, {}, {}", x, y, z);

    let pair = Pair(1.0, 2.0);
    println!("pair.0 = {}, pair.1 = {}", pair.0, pair.1);
}

fn main() {

    const PI: f64 = 3.14159;
    // test_struct();
    test_enum();

    let s: &str = "hello world";
    let owned_ptr: *const str = "hello world";

    println!("s: {}, owned_ptr: {:?}", s, owned_ptr);


    
}